
package com.example.testutil.common;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import com.example.testutil.util.LogUtil;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.support.v7.graphics.Palette;
import android.support.v7.graphics.Palette.PaletteAsyncListener;
import android.view.View;

public class CommonUtil {

    public static void traversalMap(HashMap<? extends Object, ? extends Object> map) {
        Iterator<?> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            @SuppressWarnings("unchecked")
            Map.Entry<? extends Object, ? extends Object> entry = (Entry<? extends Object, ? extends Object>) iterator
                    .next();
            entry.getKey();
            entry.getValue();
        }
    }

    public static void putShareData(String data, Context context) {
        SharedPreferences mPreferences = context.getSharedPreferences("myfile", Context.MODE_PRIVATE);
        SharedPreferences.Editor mEditor = mPreferences.edit();
        mEditor.putString("mydata", data);
        mEditor.commit();
    }

    public static String getShareData(Context context) {
        SharedPreferences mPreferences = context.getSharedPreferences("myfile", Context.MODE_PRIVATE);
        return mPreferences.getString("mydata", "default");
    }
    /*
     * 通过Palette来获取颜色,使UI整体更适合
     */
    public static void setPaletteColor(Bitmap mBitmap, final View mView) {
        Palette.from(mBitmap).generate(new PaletteAsyncListener() {

            @Override
            public void onGenerated(Palette arg0) {
                Palette.Swatch swatch = arg0.getMutedSwatch();
                // Palette.Swatch swatch2 = arg0.getDarkMutedSwatch();
                if (swatch != null) {
                    mView.setBackgroundColor(swatch.getRgb());
                }
            }
        });
    }

    public static void setMainColor(Bitmap mBitmap, final View mView) {
        Palette.Builder builder = new Palette.Builder(mBitmap);
        builder.maximumColorCount(1);
        builder.generate(new PaletteAsyncListener() {

            @Override
            public void onGenerated(Palette arg0) {
                Palette.Swatch swatch = arg0.getSwatches().get(0);
                if (swatch != null) {
                    mView.setBackgroundColor(swatch.getRgb());
                } else {
                    LogUtil.d("swatch is null ");
                }
            }
        });
    }
    
}
